import { Loading } from 'element-ui'
import '@/styles/loading.scss'

let loadingCount = 0;
let loading;

const startLoading = (title = '加载中……') => {
  loading = Loading.service({
    lock: true,
    text: title,
    background: 'rgba(0, 0, 0, 0.2)'
  });
};

const endLoading = () => {
  loading.close();
}

export const showLoading = (title) => {
  if (loadingCount === 0) {
    startLoading(title);
  }
  loadingCount += 1;
}

export const hideLoading = () => {
  if (loadingCount <= 0) {
    return;
  }
  loadingCount -= 1;
  if (loadingCount === 0) {
    endLoading();
  }
}